﻿using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using System.Xml.Linq;

namespace tranquility
{
    public class GameMode
    {
        private Game1 game;
        Matrix view;
        List<Wall> walls = new List<Wall>();
        public Lens ActiveLens { get; set; }

        public RedLens RedLens { get; set; }
        public BlueLens BlueLens { get; set; }
        public GreenLens GreenLens { get; set; }
        public Bat bat { get; set; }

        public KeyboardState LastKeyboardState { get; set; }
        public Player Player { get; private set; }

        public GameMode(Game1 game)
        {
            this.game = game;
            System.IO.Stream stream = TitleContainer.OpenStream("Content\\walls.xml");
            XDocument doc = XDocument.Load(stream);

            ActiveLens = null;

            walls = XmlReader(doc);


            RedLens = new RedLens(game, this);
            GreenLens = new GreenLens(game, this);
            BlueLens = new BlueLens(game, this);
            Player = new Player(game, new Vector3(1, 0, -4), new Vector3(1, 1, 1), 1, 270);
            bat = new Bat(game, new Vector3(500, 300, 0), new Vector3(0.3f, 1, 0), game.SpriteBatch);
            game.Components.Add(Player);
            game.Components.Add(RedLens);
            game.Components.Add(GreenLens);
            game.Components.Add(BlueLens);
            foreach (Wall wall in walls)
            {
                game.Components.Add(wall);
            }
            //Components.Add(new Wall(this, Vector3.Zero, new Vector3(2,1,0.5f)));

            game.Components.Add(new HUD(game, this));

            game.Components.Add(bat);
            game.Components.Add(new Sounds(game, this));
        }
        
        public void Update(GameTime gameTime)
        {
            KeyboardState ks = Keyboard.GetState();

            if (ks.IsKeyUp(Keys.Z) && LastKeyboardState.IsKeyDown(Keys.Z))
                ActiveLens = null;
            else if (ks.IsKeyUp(Keys.X) && LastKeyboardState.IsKeyDown(Keys.X))
                ActiveLens = RedLens;
            else if (ks.IsKeyUp(Keys.C) && LastKeyboardState.IsKeyDown(Keys.C))
                ActiveLens = GreenLens;
            else if (ks.IsKeyUp(Keys.V) && LastKeyboardState.IsKeyDown(Keys.V))
                ActiveLens = BlueLens;

            LastKeyboardState = ks;
            foreach (Wall wall in walls)
            {
                wall.lookPosition = Player.LookPoint;
                wall.cameraPosition = Player.EyePosition;
            }
        
        }
        public void Draw(SpriteBatch spriteBatch)
        {

        }

        List<Wall> XmlReader(XDocument doc)
        {
            List<Wall> wallz = new List<Wall>();
            wallz = (from wall in doc.Descendants("wall")
                     select new Wall(game, this)
                     {

                         Position = new Vector3(Convert.ToInt32(wall.Element("positionX").Value),
                                             Convert.ToInt32(wall.Element("positionY").Value),
                                             Convert.ToInt32(wall.Element("positionZ").Value)),

                         Size = new Vector3(Convert.ToInt32(wall.Element("sizeX").Value),
                                         Convert.ToInt32(wall.Element("sizeY").Value),
                                         Convert.ToInt32(wall.Element("sizeZ").Value))

                     }).ToList();
            return wallz;
        }

    }
}
